
import ws.*;
import java.io.Serializable;
import java.util.*;
import javax.faces.context.FacesContext;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;

public class viewUserBean implements Serializable {

    private UserUI selected;
    private List<StatusUI> statusUpdates;
    private List<InterestUI> interests;

    public viewUserBean() {
        statusUpdates = new ArrayList<StatusUI>();
    }

    public UserUI getSelected() {
        return selected;
    }

    public void setSelected(UserUI selected) {
        this.selected = selected;
    }

    public List<StatusUI> getStatusUpdates() {
        if (selected != null) {
            ws.RoMaWS_Service service = new ws.RoMaWS_Service();
            ws.RoMaWS port = service.getRoMaWSPort();
            //JAX-WX Auth.
            FacesContext context = FacesContext.getCurrentInstance();
            LoginBean loginBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
            Map<String, Object> req_ctx = ((BindingProvider) port).getRequestContext();
            req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WSUrl.wsUrl);
            Map<String, List<String>> headers = new HashMap<String, List<String>>();
            headers.put("Username", Collections.singletonList(loginBean.getName()));
            headers.put("Password", Collections.singletonList(loginBean.getPassword()));
            req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
            return port.getStatusUpdates(selected.getId());
        }
        return new ArrayList<StatusUI>();
    }

    public void setStatusUpdates(List<StatusUI> statusUpdates) {
        this.statusUpdates = statusUpdates;
    }

    public List<InterestUI> getInterests() {
        ws.RoMaWS_Service service = new ws.RoMaWS_Service();
        ws.RoMaWS port = service.getRoMaWSPort();
        //JAX-WX Auth.
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean loginBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        Map<String, Object> req_ctx = ((BindingProvider) port).getRequestContext();
        req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WSUrl.wsUrl);
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        headers.put("Username", Collections.singletonList(loginBean.getName()));
        headers.put("Password", Collections.singletonList(loginBean.getPassword()));
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        interests = port.getUserInterests(selected.getId());
        return interests;
    }

    public void setInterests(List<InterestUI> interests) {
        this.interests = interests;
    }
}
